
import type { AxiosRequestConfig } from "axios";
import axios  from "axios";
import type { ResponeData } from "@/types/HttpType";
import { message } from 'ant-design-vue';
import { useTokenStore } from '@/stores/token'
import { useCookies } from "vue3-cookies";
const { cookies } = useCookies();

axios.defaults.timeout = 30000;

axios.defaults.withCredentials = false // 跨域请求，允许保存cookie

// 添加请求拦截器
axios.interceptors.request.use(function (config) {

  config.headers['Authorization'] = cookies.get('Authorization') 
  return config

}, function (error) {
  console.log('error' + error)
  return Promise.reject(error)
})

axios.interceptors.response.use(
  function(response){
    
    
   
    return response
  },
  function (err){
    console.log(err)
    if(err.response.status == 401){
      cookies.remove('Authorization')
      const tokenStore = useTokenStore()
      tokenStore.toLogin()
    }
    
    return Promise.reject(err.response.data)
  }
)




export default <T>(options: AxiosRequestConfig): Promise<ResponeData<T>> => {
     return new Promise((resolve, reject) => {
       axios({
        ...options
       }).then(res => {
         
         resolve(res.data as ResponeData<T>)
       }).catch(err => {
          reject(err)
       })
     })
   
   
   }